<template>

  <div>

    <span style="color: grey">当前位置 >> 用户管理 >> 用户信息查询</span>

    <div class="box" style="display: inline-block">
      <div class="searchBox" style="display: flex">

        <div style="margin: 10px 0;display: flex">
          <el-input v-model="search" placeholder="输入关键字查询" style="width: 400px;" @clear="load" @keyup.enter="load"
                    clearable></el-input>
          <el-button style="margin-left: 10px" type="primary" @click="load" circle>
            <el-icon>
              <search/>
            </el-icon>
          </el-button>
        </div>
        <div style="margin: 10px 30px">
          <el-select v-model="sort" placeholder="请选择搜索类型" style="margin-left: 20px;width: 200px" @change="load">
            <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value">
            </el-option>
          </el-select>
        </div>
      </div>
    </div>

    <div class="block" style="margin-top: 30px" v-loading="loadering">

      <el-table :data="information" style="width: 98%" border>
        <el-table-column fixed prop="pno" label="职工号"/>
        <el-table-column prop="pname" label="姓名"/>

        <el-table-column prop="psex"
                         label="性别"

        >
          <template #default="scope">
            <el-tag
                type="success"
                v-if="scope.row.psex === '1'"
                disable-transitions>
              男
            </el-tag>

            <el-tag
                v-if="scope.row.psex === '0'"
                disable-transitions>
              女
            </el-tag>
          </template>
        </el-table-column>

        <el-table-column prop="pbirth" label="出生日期"/>
        <el-table-column prop="competent" label="职称"/>
        <el-table-column prop="department" label="所在部门"/>


        <el-table-column fixed="right" label="操作" width="200px">
          <template #default="scope">
            <el-row>
              <el-col :span="6">
                <el-button size="small" type="success" @click="handleFind(scope.row)">查看</el-button>
              </el-col>
              <el-col :span="6" style="margin-left: 10px">
                <el-button size="small" type="primary" @click="handleEdit(scope.row)">修改</el-button>
              </el-col>
              <el-col :span="6" style="margin-left: 10px">
                <el-popconfirm title="确认删除吗?" @confirm="handleDrop(scope.row)">
                  <template #reference>
                    <el-button
                        size="small"
                        type="danger">删除
                    </el-button>
                  </template>
                </el-popconfirm>
              </el-col>
            </el-row>
          </template>
        </el-table-column>

      </el-table>


      <el-pagination
          v-model:currentPage="currentPage"
          :page-sizes="[5, 10, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          class="mt-4"
          background

      >
      </el-pagination>
    </div>

    <el-drawer
        v-model="drawer2"
        title="详情"
        :close-on-click-modal=false
    >
      <el-scrollbar height="100%">
        <el-form :model="ruleForm" status-icon ref="ruleForm" label-width="100px" class="demo-ruleForm"
                 :rules="formRules"
                 style="margin-right: 20px;"
        >
          <el-form-item label="职工号" prop="pno" style="display: flex">
            <el-input type="text" v-model="ruleForm.pno" autocomplete="off" @blur.prevent="changepno()"></el-input>
            <span v-if="this.torf === false" style="font-size: 10px;color: red">{{ tSpan }}</span>
          </el-form-item>
          <el-form-item label="姓名" prop="pname">
            <el-input type="text" v-model="ruleForm.pname" autocomplete="off"></el-input>
          </el-form-item>

          <el-form-item label="性别" prop="psex">
            <el-switch
                style="margin-left: 20px"
                v-model="ruleForm.psex"
                active-text="女"
                inactive-text="男"
                active-value="0"
                inactive-value="1">
            </el-switch>
          </el-form-item>

          <el-form-item label="籍贯" prop="pnative">
            <el-input type="text" v-model="ruleForm.pnative" autocomplete="off"></el-input>
          </el-form-item>

          <el-form-item label="出生日期" prop="pbirth">
            <el-date-picker v-model="ruleForm.pbirth"
                            type="date" style="width: 100%" placeholder="请选择出生日期">
            </el-date-picker>
          </el-form-item>

          <el-form-item label="职称" prop="competent">
            <el-input type="text" v-model="ruleForm.competent" autocomplete="off"></el-input>
          </el-form-item>

          <el-form-item label="所学专业" prop="discipline">
            <el-input type="text" v-model="ruleForm.discipline" autocomplete="off"></el-input>
          </el-form-item>

          <el-form-item label="现从事专业" prop="dodiscipline">
            <el-input type="text" v-model="ruleForm.dodiscipline" autocomplete="off"></el-input>
          </el-form-item>

          <el-form-item label="学历" prop="education">
            <el-input type="text" v-model="ruleForm.education" autocomplete="off"></el-input>
          </el-form-item>

          <el-form-item label="学位" prop="degree">
            <el-input type="text" v-model="ruleForm.degree" autocomplete="off"></el-input>
          </el-form-item>

          <el-form-item label="工作时间" prop="hworking">
            <el-input type="text" v-model="ruleForm.hworking" autocomplete="off"></el-input>
          </el-form-item>

          <el-form-item label="所在部门" prop="department">
            <el-input type="text" v-model="ruleForm.department" autocomplete="off"></el-input>
          </el-form-item>

          <el-form-item label="照片" prop="image">
            <el-upload
                class="upload-demo"
                drag
                action="http://222.21.119.252:9999/files/upload"
                multiple
                :on-success="filesUploadSuccess"
                :on-error="filesUploadError"
                :before-upload="checkFileType"
                style="width: 400px"
                :data=fileVO
                :limit="1"
                :on-exceed='limitCheck'
                :file-list="urlList"
            >
              <el-icon class="el-icon--upload">
                <upload-filled/>
              </el-icon>
              <div class="el-upload__text">
                拖拽上传 或 <em>点击上传</em>
              </div>
              <template #tip>
                <div class="el-upload__tip">
                  上传的图片文件会自动重命名为职工号,故请先输入职工号;
                </div>
              </template>
            </el-upload>
          </el-form-item>

          <el-form-item label="备注" prop="remark">
            <el-input type="textarea"
                      :autosize="{ minRows: 2, maxRows: 4}"
                      placeholder="请输入内容" v-model="ruleForm.remark" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button :disabled="!torf" type="primary" @click="editForm"
                       style="width:100%; height:100%;margin-bottom:15px;">提交
            </el-button>
          </el-form-item>
        </el-form>

      </el-scrollbar>


    </el-drawer>

    <el-drawer
        v-model="findDrower"
        title="详情"
    >
      <el-scrollbar >
        <el-descriptions :column="1" border>
          <el-descriptions-item
              label="id"
              label-align="right"
              align="center">
            {{ this.findFrom.id }}
          </el-descriptions-item>

          <el-descriptions-item label="职工号" label-align="right" align="center"> {{ this.findFrom.pno }}
          </el-descriptions-item>

          <el-descriptions-item label="姓名" label-align="right" align="center"> {{ this.findFrom.pname }}
          </el-descriptions-item>

          <el-descriptions-item label="性别" label-align="right" align="center">
            <el-tag v-if="findFrom.psex === '1'" type="success">
              男
            </el-tag>
            <el-tag v-else>
              女
            </el-tag>
          </el-descriptions-item>

          <el-descriptions-item label="籍贯" label-align="right" align="center">
            {{ this.findFrom.pnative }}
          </el-descriptions-item>


          <el-descriptions-item label="出生日期" label-align="right" align="center">
            {{ this.findFrom.pbirth }}
          </el-descriptions-item>

          <el-descriptions-item label="职称" label-align="right" align="center">
            {{ this.findFrom.competent }}
          </el-descriptions-item>

          <el-descriptions-item label="所学专业" label-align="right" align="center">
            {{ this.findFrom.discipline }}
          </el-descriptions-item>

          <el-descriptions-item label="现从事专业" label-align="right" align="center">
            {{ this.findFrom.dodiscipline }}
          </el-descriptions-item>

          <el-descriptions-item label="学历" label-align="right" align="center">
            {{ this.findFrom.education }}
          </el-descriptions-item>

          <el-descriptions-item label="学位" label-align="right" align="center">
            {{ this.findFrom.degree }}
          </el-descriptions-item>

          <el-descriptions-item label="工作时间" label-align="right" align="center">
            {{ this.findFrom.hworking }}
          </el-descriptions-item>


          <el-descriptions-item label="所在部门" label-align="right" align="center" v-if="findFrom.appraise === '1'">
            {{ this.findFrom.appraisetime }}
          </el-descriptions-item>

          <el-descriptions-item label="照片" label-align="right" align="center">

            <template #default="scope">
              <el-image
                  style="width: 80px; height: 80px"
                  :src="findFrom.image"
              >
              </el-image>
            </template>
          </el-descriptions-item>

          <el-descriptions-item label="备注" label-align="right" align="center" >
            {{ this.findFrom.remark }}
          </el-descriptions-item>

        </el-descriptions>
      </el-scrollbar>

    </el-drawer>
  </div>
</template>
<script>
import request from "@/utils/request";
import {Search, Edit, Delete, UploadFilled} from '@element-plus/icons-vue'


export default {
  name: "SearchUser",
  components: {
    Search,
    Edit,
    Delete,
    UploadFilled
  },
  data() {

    return {
      findFrom: {},
      urlList: [{
        name: '',
        value: ''
      }],
      fileVO: {
        sort: 'user',
        fileName: ''
      },
      drawer2: false,
      findDrower: false,
      torf: true,
      tSpan: '该职工号已存在',
      ruleForm: {
        pno: '',
        pname: '',
        psex: '',
        pnative: '',
        pbirth: '',
        competent: '',
        discipline: '',
        dodiscipline: '',
        education: '',
        degree: '',
        hworking: '',
        department: '',
        image: '',
        remark: ''
      },
      formRules: {
        pno: [
          {required: true, message: '请输入职工号', trigger: 'blur'}
        ],
        pname: [
          {required: true, message: '请输入姓名', trigger: 'blur'}
        ],
        department: [
          {required: true, message: '请输入所在部门', trigger: 'blur'}
        ],
        pnative: [
          {required: true, message: '请输入籍贯', trigger: 'blur'}
        ],
        pbirth: [
          {required: true, message: '请选择出生日期', trigger: 'blur'}
        ],
        competent: [
          {required: true, message: '请输入职称', trigger: 'blur'}
        ],
        discipline: [
          {required: true, message: '请输入所学专业', trigger: 'blur'}
        ],
        dodiscipline: [
          {required: true, message: '请输入先从事专业', trigger: 'blur'}
        ],
        education: [
          {required: true, message: '请输入学历', trigger: 'blur'}
        ],
        degree: [
          {required: true, message: '请输入学位', trigger: 'blur'}
        ],
        hworking: [
          {required: true, message: '请输入工作时间', trigger: 'blur'}
        ],
        image: [
          {required: true, message: '请上传照片', trigger: 'blur'}
        ],
      },
      loadering: true,
      pno: '',
      options: [
        {
          value: 'Pno',
          label: '职工号'
        },
        {
          value: 'Pname',
          label: '姓名'
        },
        {
          value: 'Competent',
          label: '职称'
        },
        {
          value: 'Department',
          label: '所在部门'
        },
      ],

      //下载路径
      fileUrl: '',
      //数据
      information: [],
      currentPage: 1,
      total: 0,
      search: '',
      pageSize: 10,
      sort: ''
    };
  },
  methods: {
    handleFind(row) {
      //深拷贝
      this.findFrom = JSON.parse(JSON.stringify(row));

      this.findDrower = true;
    },
    downthis1(file) {
      window.location.href = file;
    },
    load() {
      request.get(`/sperson`, {
        params: {
          pageNum: this.currentPage,
          pageSize: this.pageSize,
          search: this.search,
          sort: this.sort
        }
      }).then(res => {
        if (res.code === '200') {
          this.information = res.data.records;
          this.total = res.data.total;
          this.loadering = false;
        }
      }).catch(res => {
        this.$message.error("服务器错误,请联系管理员或重新尝试");
      })
    },
    //点击表格上的修改
    handleEdit(row) {
      //深拷贝
      this.ruleForm = JSON.parse(JSON.stringify(row));
      this.pno = this.ruleForm.pno;
      //显示文件列表
      this.urlList[0].name = '原文件';
      this.urlList[0].uri = this.ruleForm.image;
      this.drawer2 = true;
    },
    handleDrop(id) {
      request.delete("/sperson/" + id).then(res => {
        if (res.code === '200') {
          //弹出成功框
          this.$message.success("删除成功")
          this.load();
        }
      }).catch(() => {   //防止未知bug,执行不了Controller方法直接报错
            this.$message.error("服务器错误!请联系管理员或重新尝试!");
          }
      )
    },
    //点击修改的提交按钮
    editForm() {
      this.$refs.ruleForm.validate(valid => {
        if (!valid) {
          this.$message.error("请输入必选项!")
          return
        }
        request.put("/sperson", this.ruleForm).then(res => {
          if (res.code === '200') {
            //弹出成功框
            this.$message.success("修改成功")
            this.load();
            this.drawer2 = false;
          } else {
            this.$message.error("修改失败!请确保表单各项填写完整正确!");
          }
        }).catch(() => {   //防止未知bug,执行不了Controller方法直接报错
          this.$message.error("服务器错误!请联系管理员或重新尝试!");
        })
      })
    },
    handleSizeChange(pageSize) {
      this.pageSize = pageSize;
      this.load();
    },
    handleCurrentChange(pageNum) {
      this.currentPage = pageNum;
      this.load();
    },

    //用来检查职工号是否重复
    changepno() {
      if (this.ruleForm.pno === '') {
        this.torf = true;
      } else {
        request.get(`/sperson/pno/` + this.ruleForm.pno).then(res => {
          if (res.code === '200') {
            //表示不重复 则可以用
            this.torf = true;
          } else {
            if (this.pno === res.data.pno) {
              this.torf = true;
            } else {
              //重复 不可以用
              this.torf = false;
            }

          }
        })
      }
    },
    filesUploadSuccess(res) {
      this.ruleForm.image = res.data;
    },
    checkFileType(file) {
      // 以检查文件是否为.jpg为例
      const fileNameName = file.name;
      const fileType = fileNameName.substring(fileNameName.lastIndexOf('.'));
      if (fileType === '.jpg' || fileType === '.jpeg' || fileType === '.png') {
        if (this.ruleForm.pno === '') {
          this.$message.error("上传文件前请先输入职工号!");
          return false;
        }
      } else {
        this.$message.error("仅支持上传 jpg,jpeg,png 类型的图片")
        return false;
      }

      if (file.size / 1024 / 1024 > 100) {
        this.$message.error("文件大小不得超过100MB");
        return false;
      }

      //文件命名
      this.fileVO.fileName = this.ruleForm.pno;
    },
    limitCheck() {
      this.$message.warning('只能上传一个文件');
    },
    filesUploadError(res) {
      if (res.code === '201') {
        this.$message.error(res.data.msg);
      } else {
        this.$message.error("上传失败,请重新尝试或联系管理员");
      }
    },
  },
  created() {
    this.load();
  }
}
</script>

<style scoped>
.box {
  margin: 0 auto;
  padding-top: 20px;
  height: 50px;
  width: 100%;
}

.searchBox {
  margin: 0 auto;
  width: 60%;
  position: relative;
}

/deep/ .el-upload .el-upload-dragger {
  width: 300px;
}

</style>
